package com.spooky.sync.note;

import java.util.LinkedList;
import java.util.List;
import android.app.ExpandableListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.database.DataSetObserver;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ExpandableListAdapter;
import android.widget.TextView;
import com.spooky.sync.note.dialog.ViewNoteDialog;
import com.spooky.sync.persistence.dao.NoteDAO;
import com.spooky.sync.persistence.dao.NoteDAOBean;
import com.spooky.sync.persistence.entity.ExpandableNote;

public class NoteListActivity extends ExpandableListActivity {
	private static final String TAG = "ListActivity";
	// GUI
	private Button mNext = null;
	private Button mPrevious = null;
	private Context mContext;
	private DatabaseProgress mDatabaseProgress;
	// Storage
	private List<ExpandableNote> mExpandableNotes;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.note_list);
		mContext = getApplicationContext();
		initate();
	}

	@Override
	protected void onResume() {
		super.onResume();
		GetAllNotes getAllNotes = new GetAllNotes();
		getAllNotes.execute();
	}

	@Override
	protected void onPause() {
		super.onPause();
		dialogOnPause();
	}

	private void initate() {
		// mNext = (Button) findViewById(R.id.list_next);
		// mPrevious = (Button) findViewById(R.id.list_previous);
	}

	protected void onPrevious(View view) {
	}

	protected void onNext(View view) {
	}

	private class CustomAdapter implements ExpandableListAdapter {
		@Override
		public boolean areAllItemsEnabled() {
			return true;
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return mExpandableNotes.get(groupPosition).getNote().get(childPosition);
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return mExpandableNotes.get(groupPosition).getNote().get(childPosition).getId();
		}

		@Override
		public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
			TextView textView = new TextView(mContext);
			textView.setText(mExpandableNotes.get(groupPosition).getNote().get(childPosition).getName());
			textView.setOnLongClickListener(new OnLongClickListener() {
				@Override
				public boolean onLongClick(View v) {
					Log.d(TAG, "onLongClick");
					return false;
				}
			});
			textView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Log.d(TAG, "onClick");
					ViewNoteDialog dialog = new ViewNoteDialog(NoteListActivity.this);
					dialog.setTitle(mExpandableNotes.get(groupPosition).getNote().get(childPosition).getName());
					dialog.setText(mExpandableNotes.get(groupPosition).getNote().get(childPosition).getText());
					dialog.show();
				}
			});
			return textView;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return mExpandableNotes.get(groupPosition).getNote().size();
		}

		@Override
		public long getCombinedChildId(long groupId, long childId) {
			return childId;// ?
		}

		@Override
		public long getCombinedGroupId(long groupId) {
			return groupId;// ?
		}

		@Override
		public Object getGroup(int groupPosition) {
			return mExpandableNotes.get(groupPosition);
		}

		@Override
		public int getGroupCount() {
			return mExpandableNotes.size();
		}

		@Override
		public long getGroupId(int groupPosition) {
			Log.d(TAG, groupPosition + "");
			mExpandableNotes.get(groupPosition);
			return mExpandableNotes.get(groupPosition).getId();
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
			TextView textView = new TextView(mContext);
			textView.setText(mExpandableNotes.get(groupPosition).getName());
			return textView;
		}

		@Override
		public boolean hasStableIds() {
			return true;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}

		@Override
		public boolean isEmpty() {
			return mExpandableNotes.isEmpty();
		}

		@Override
		public void onGroupCollapsed(int groupPosition) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onGroupExpanded(int groupPosition) {
			// TODO Auto-generated method stub
		}

		@Override
		public void registerDataSetObserver(DataSetObserver observer) {
			// TODO Auto-generated method stub
		}

		@Override
		public void unregisterDataSetObserver(DataSetObserver observer) {
			// TODO Auto-generated method stub
		}
	}

	private class GetAllNotes extends AsyncTask<String, Integer, List<ExpandableNote>> {
		private NoteDAO mNoteDAO;

		public GetAllNotes() {
		}

		@Override
		protected List<ExpandableNote> doInBackground(String... params) {
			Log.d(TAG, "doInBackground");
			try {
				return mNoteDAO.listExpandable();
			} catch (Exception e) {
				return new LinkedList<ExpandableNote>();
			}
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
			Log.d(TAG, "onCancelled");
			mNoteDAO.close();
		}

		@Override
		protected void onPostExecute(List<ExpandableNote> result) {
			super.onPostExecute(result);
			Log.d(TAG, "onPostExecute");
			mExpandableNotes = result;
			mNoteDAO.close();
			setListAdapter(new CustomAdapter());
			mDatabaseProgress.cancel();
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			Log.d(TAG, "onPreExecute");
			mNoteDAO = new NoteDAOBean(NoteListActivity.this);
			mDatabaseProgress = new DatabaseProgress();
			mDatabaseProgress.show();
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
			Log.d(TAG, "onProgressUpdate");
			setProgress(values[0]);
		}
	}

	private class DatabaseProgress extends ProgressDialog {
		public DatabaseProgress() {
			super(NoteListActivity.this);
			setMessage("Loading... Please wait");
		}
	}

	private void dialogOnPause() {
		if (mDatabaseProgress != null) {
			if (mDatabaseProgress.isShowing()) {
				mDatabaseProgress.dismiss();
			}
		}
	}
}
